package com.szhuang.mobilesafe.db.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by szhuang on 2015/3/28.
 */
public class NumberAddressQueryUtils {

    private static String path="data/data/com.szhuang.mobilesafe/files/address.db";

    public static String queryNumber(String number){

        String address=number;
        SQLiteDatabase database=SQLiteDatabase.openDatabase(path,null,SQLiteDatabase.OPEN_READONLY);

        if(number.matches("^1[34568]\\d{9}$")){

            Cursor cursor=database.rawQuery("select location from data2 where id=(select outkey from data1 where id=?)",new String[]{number.substring(0,7)});
            while(cursor.moveToNext()){
                String location=cursor.getString(0);
                address=location;
            }
            cursor.close();
        }else{
            switch(number.length()){
                case 3:
                    address="匪警号码";
                    break;
                case 4:
                    address="模拟器号码";
                    break;
                case 5:
                    address="客服电话";
                    break;
                case 7:
                    address="本地号码";
                    break;
                case 8:
                    address="本地号码";
                    break;
                default:
                    if(number.length()>10&&number.startsWith("0")){
                        Cursor cursor=database.rawQuery("select location from data2 where area=?",new String[]{number.substring(1,3)});
                        while (cursor.moveToNext()){
                            String location=cursor.getString(0);
                            address=location.substring(0,location.length()-2);
                        }
                        cursor.close();
                        cursor=database.rawQuery("select location from data2 where area=?",new String[]{number.substring(1,4)});
                        while (cursor.moveToNext()){
                            String location=cursor.getString(0);
                            address=location.substring(0,location.length()-2);
                        }
                        cursor.close();
                    }
                    break;
            }
        }

        return address;
    }
}
